package com.zaicheng.cloud.netty;
/**
 @author zaicheng
 @version 1.0
 @date 2022-04-19
 @email zzc15847256473@163.com
 */

import com.zaicheng.cloud.remotecall.RequestTemplate;

import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

public class RpcRequest<T> {

        private final String url;
        private final RpcRequest.Body body;
        private final RequestTemplate requestTemplate;

        public RpcRequest(String url, Body body, RequestTemplate requestTemplate) {
            this.url = url;
            this.body = body;
            this.requestTemplate = requestTemplate;
        }

        public static RpcRequest create(String url, RpcRequest.Body body, RequestTemplate requestTemplate) {
            return new RpcRequest(url,body, requestTemplate);
        }

        public  static  class  Body{

            private Charset encoding;

            private byte[] data;

            public Body() {
            }

            public Body(byte[] data) {
                this.data = data;
            }

            public Body(Charset encoding, byte[] data) {
                this.encoding = encoding;
                this.data = data;
            }

            public Optional<Charset> getEncoding(){
                return  Optional.ofNullable(this.encoding);
            }

            public int length() {
                return this.data != null ? this.data.length : 0;
            }

            public byte[] asBytes() {
                return this.data;
            }

            public  static  RpcRequest.Body  create(String data){
                return new RpcRequest.Body(data.getBytes());
            }

            public  static  RpcRequest.Body create(String data,Charset charset){
                return  new RpcRequest.Body(charset, data.getBytes());
            }

            public static RpcRequest.Body create(byte[] data) {
                return new RpcRequest.Body(data);
            }

            public static RpcRequest.Body create(byte[] data, Charset charset) {
                return new RpcRequest.Body(charset, data);
            }

            public static RpcRequest.Body empty() {
                return new RpcRequest.Body();
            }
        }

        public  static  class  Options{

            private  final  long connectTimeout;

            private  final TimeUnit  connectTimeoutUnit;

            private  final  long readTimeout;

            private  final TimeUnit  readTimeoutUnit;


            public Options(long connectTimeout, TimeUnit connectTimeoutUnit, long readTimeout, TimeUnit readTimeoutUnit) {
                this.connectTimeout = connectTimeout;
                this.connectTimeoutUnit = connectTimeoutUnit;
                this.readTimeout = readTimeout;
                this.readTimeoutUnit = readTimeoutUnit;
            }

            public Options() {
                this(10L,TimeUnit.SECONDS,60L,TimeUnit.SECONDS);
            }

            public  int  connectTimeoutMills(){
                return (int) this.connectTimeoutUnit.toMillis(this.connectTimeout);
            }

            public  int readTimeoutMills(){
                return (int) this.connectTimeoutUnit.toMillis(this.readTimeout);
            }

            public long getConnectTimeout() {
                return connectTimeout;
            }

            public TimeUnit getConnectTimeoutUnit() {
                return connectTimeoutUnit;
            }

            public long getReadTimeout() {
                return readTimeout;
            }

            public TimeUnit getReadTimeoutUnit() {
                return readTimeoutUnit;
            }
        }

        public static enum HttpMethod {
            GET,
            HEAD,
            POST,
            PUT,
            DELETE,
            CONNECT,
            OPTIONS,
            TRACE,
            PATCH;

            private HttpMethod() {
            }
        }

}
